// 导入模板文件下载
export const handleImportTemplate = async (fielname, api) => {
  const { data, headers } = await api();
  const blob = new Blob([data], { type: headers["content-type"] });
  const fileName = fielname;
  const elink = document.createElement("a");
  elink.download = fileName;
  elink.style.display = "none";
  elink.href = URL.createObjectURL(blob);
  document.body.appendChild(elink);
  elink.click();
  URL.revokeObjectURL(elink.href); // 释放URL 对象
};

// 防抖
export function debounce(fn, wait) {
  let timeout = null;
  wait = wait || 600;
  return function () {
    let that = this;
    if (timeout !== null) clearTimeout(timeout);
    timeout = setTimeout(() => {
      fn.apply(that);
    }, wait);
  };
}

// 获取sessionStorage
export const getSessionStorage = (key) => {
  if (sessionStorage.getItem(key)) {
    return JSON.parse(sessionStorage.getItem(key));
  } else {
    return null;
  }
};
// 设置sessionStorage
export const setSessionStorage = (key, val) => {
  sessionStorage.setItem(key, JSON.stringify(val));
};
// 清除sessionStorage
export const clearSessionStorage = (key) => {
  sessionStorage.removeItem(key);
};

// 获取localStorage
export const getLocalStorage = (key) => {
  if (localStorage.getItem(key)) {
    return JSON.parse(localStorage.getItem(key));
  } else {
    return null;
  }
};
// 设置localStorage
export const setLocalStorage = (key, val) => {
  localStorage.setItem(key, JSON.stringify(val));
};
// 清除localStorage
export const clearLocalStorage = (key) => {
  localStorage.removeItem(key);
};
